package com.example.exam.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.example.exam.utils.Md5Utils;
import com.example.exam.utils.RandomUtils;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户登录的表
 * </p>
 *
 * @author author
 * @since 2024-03-28
 */
@Data
@TableName("exam.user")
public class User implements Serializable {
    private static final Integer USE = 1;
    private static final Integer STOP = 0;
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @TableField("`account`")
    private String account;

    @TableField("`name`")
    private String name;

    /**
     * MD5加密
     */
    @TableField("`password`")
    private String password;

    /**
     * 添加时间
     */
    @TableField("`time`")
    private LocalDateTime time;

    /**
     * 1.激活 2.禁用
     */
    @TableField("`status`")
    private Integer status;

    /**
     * 性别
     */
    private Integer sex;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 邮箱
     */
    private String email;

    /**
     * 盐
     */
    private String salt;

    private String idcard;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 新用户初始化
     *
     *
     */
    public void userInit() {
        setStatus(USE);
        setTime(LocalDateTime.now());
        setSalt(RandomUtils.generateSalt());
        setAccount(RandomUtils.generateAccount());
        setNickname(name);
        setPassword(Md5Utils.toHex(Md5Utils.md5(this.idcard.substring(12, 18), this.salt)));
        setIdcard(Md5Utils.toHex(Md5Utils.md5(this.idcard, this.salt)));
    }


    public void setPassword(String source) {
        this.password = Md5Utils.toHex(Md5Utils.md5(source,salt));
    }

    public String getDecryptPassword() {
        return Md5Utils.deciphering(password,salt);
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", account=" + account +
                ", name=" + name +
                ", password=" + password +
                ", time=" + time +
                ", status=" + status +
                ", sex=" + sex +
                ", age=" + age +
                ", email=" + email +
                ", salt=" + salt +
                ", idcard=" + idcard +
                ", nickname=" + nickname +
                "}";
    }
}
